<?php
// 本类由系统自动生成，仅供测试用途
class FileAction extends CommonAction {
	public function _before_index() {
		/* $model = M ( "File" );
		$list = $model->getField ( 'id,depname,depdesc,deplevel,fax' );
		$this->assign ( 'groupList', $list ); */
	}
	public function _before_add() {
		$model = M ( "User" );
		$list = $model->select ();
		$this->assign ( 'list', $list );
	}
	 function insert() {
		//B('FilterString');
		$name=$this->getActionName();
		$model = D ($name);
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}		
		$fileinfo=$this->uploadFile();
		$model->name = $fileinfo[0]['name']; // 上传文件的原始名称
		$model->path = $fileinfo[0]['savepath']; // 上传文件的保存路径
		$model->extension = $fileinfo[0]['extension']; // 上传文件的后缀类型
		$model->size = ($fileinfo[0]['size']/1024).'Kb'; // 文件大小
		$model->savename=$fileinfo[0]['savename'];
		$list=$model->add ();		
		if ($list!==false) { //保存成功
			$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
			$this->ajaxReturn (1,'新增成功!',200);
		} else {
			//失败提示
			$this->ajaxReturn (1,'新增失败!',300);
		}
	} 
	public function uploadFile(){
		//完成与thinkphp相关的，文件上传类的调用
		import('@.Org.UploadFile');//将上传类UploadFile.class.php拷到Lib/Org文件夹下
		$upload=new UploadFile();
		$upload->maxSize = 3145728 ; // 设置附件上传大小 
		$upload->savePath='./Public/Uploads/'.date('Ymd').'/';//保存路径建议与主文件平级目录或者平级目录的子目录来保存		
		$upload->allowExts=array('jpg','jpeg','png','gif','pdf','doc','txt');//准许上传的文件类型		
		if($upload->upload()){
			$info=$upload->getUploadFileInfo();	
			return $info;		
		}else{
			$this->error($upload->getErrorMsg());//专门用来获取上传的错误信息的
		}		
	}
	public function download(){
		$uploadPath = './Public/'; //设置文件上传路径，服务器上的绝对路径
		$id = $_GET['id']; //GET方式传到此方法中的参数id,即文件在数据库里的保存id.根据之查找文件信息。
		if($id == "")  //如果id为空而出错时，程序跳转到项目的Index/index页面。或可做其他处理。
		{
			echo '<script language="javascript">alert("没有此文件");</script>';
		}
		 
		$file = D("File"); //利用与表file对应的数据模型类FileModel来建立数据对象。
		$map = array( 'id' => $id );
		$result = $file->where($map)->select();   //根据id查询到文件信息
		 
		if($result==false) //如果查询不到文件信息而出错时，程序跳转到项目的Index/index页面,或可做其他处理
		{
			$this->redirect('index','Index',"APP_NAME",1);
		}else {
			$path=$result[0]['path'];//路径
			$savename = $result[0]['savename'];  //文件保存名
			$showname = $result[0]['name'];  //文件原名
			$filename = $uploadPath.$path.$savename; //完整文件名（路径加名字）
			$durl = $filename;
			$filename = iconv("utf-8","gbk",$showname);
			$file = @fopen($durl, 'r');
			header("Content-Type:text/html;charset=utf-8");
			header("Content-Type:application/force-download");
			header("Content-Type: application/octet-stream");
			header("Content-Type:application/download");
			header("Content-Transfet-Encoding:binary");
			header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
			header("Accept-Ranges:    bytes");
			header("Accept-Length:    ".filesize($durl));
			header('Content-Disposition:    attachment;    filename="'.$filename.'"');
			
			echo   fread($file,filesize($durl));
			fclose($file);
		}
	}	
	public function _before_edit() {
		$model = M ( "File" );
		$list = $model->select ();
		$this->assign ( 'list', $list );
	}
}